package com.xckj.teacher.ui.activity

import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.xckj.teacher.R
import com.xckj.teacher.databinding.ActivityLoginBinding
import com.xckj.teacher.framework.BaseActivity
import com.xckj.teacher.presenter.LoginPresenter
import com.xckj.teacher.ui.appViewModel
import com.xckj.teacher.util.ActivityMessenger
import com.xckj.teacher.util.CacheUtil
import com.xckj.teacher.util.CornerTransform
import com.xckj.teacher.util.SettingUtil
import com.xckj.teacher.view.ProtocolDialog
import kotlin.system.exitProcess


class LoginActivity : BaseActivity<ActivityLoginBinding>() {

    private val loginPresenter: LoginPresenter by lazy { LoginPresenter(this) }

    inner class ProxyClick {

        fun reset() {
            captcha()
        }

        fun login() {
            if (!dataBinding.cb.isChecked) {
                ToastUtils.showShort("请先阅读并同意使用协议及隐私政策")
                return
            }
            if (loginPresenter.username.value?.isEmpty() == true || loginPresenter.pass.value?.isEmpty() == true
                || loginPresenter.cap.value?.isEmpty() == true
            ) {
                return
            }
            loginPresenter.login({
                ActivityMessenger.startActivity<HomeActivity>(this@LoginActivity)
                finish()
            }, {
                captcha()
            })

        }

    }

    override fun loadLayout(): Int {
        return R.layout.activity_login
    }

    override fun initData() {
        dataBinding.presenter = loginPresenter
        var user = CacheUtil.getLoginUser()
        if (user != null) {
            loginPresenter.username.value = user.username
            loginPresenter.pass.value = user.password
        }

        captcha()
        loginPresenter.getAbout()
        dataBinding.click = ProxyClick()
        if (CacheUtil.isFirst()) {
            initProtocolDialog();
        }

    }

    private fun initProtocolDialog() {
        val protocolDialog = ProtocolDialog(this, R.style.CustomDialogStyle)
        val window: Window = protocolDialog.window!!
        val localLayoutParams = window.attributes
        localLayoutParams.gravity = Gravity.BOTTOM
        window.setGravity(Gravity.BOTTOM)
        window.decorView.setPadding(0, 0, 0, 0)
        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
        localLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
        protocolDialog.window!!.attributes = localLayoutParams
        protocolDialog.setNotAgreeClickListener {
            exitProcess(0)
        }
        protocolDialog.setAgreeClickListener {
            CacheUtil.setFirst(false)
            protocolDialog.dismiss()
        }
        protocolDialog.setLookProtocolClickListenerOne {
            appViewModel.about.observe(this) {
                ActivityMessenger.startActivityForResult(
                    this@LoginActivity, ProtocolActivity::class,
                    "data" to it.teacher_use_agreement,
                    "name" to "使用协议"
                ) {}
            }
        }
        protocolDialog.setLookProtocolClickListenerTwo {
            appViewModel.about.observe(this) {
                ActivityMessenger.startActivityForResult(
                    this@LoginActivity, ProtocolActivity::class,
                    "data" to it.teacher_privacy_agreement,
                    "name" to "隐私政策"
                ) {}
            }

        }

        protocolDialog.setLookProtocolClickListenerThree {
            appViewModel.about.observe(this) {
                ActivityMessenger.startActivityForResult(
                    this@LoginActivity, ProtocolActivity::class,
                    "data" to it.teacher_protect_agreement,
                    "name" to "儿童隐私协议"
                ) {}
            }
        }

        protocolDialog.show()
    }

    private fun captcha() {
        loginPresenter.getCaptcha { ra, captchaByte ->
            val transformation = CornerTransform(
                this,
                SettingUtil.dp2px(6f).toFloat()
            )
            transformation.setExceptCorner(
                leftTop = true,
                rightTop = false,
                leftBottom = true,
                rightBottom = false
            )

            Glide.with(this).load(captchaByte)
                .apply(RequestOptions.bitmapTransform(transformation))
                .into(dataBinding.ivCaptcha)

        }
    }

    override fun setListener() {
        appViewModel.about.observe(this) {
            val useSpan = object : ClickableSpan() {
                override fun onClick(widget: View) {
                    ActivityMessenger.startActivityForResult(
                        this@LoginActivity, ProtocolActivity::class,
                        "data" to it.teacher_use_agreement,
                        "name" to "使用协议"
                    ) {
                        if (it != null) {


                        }
                    }
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = resources?.getColor(R.color._2979ff)!!
                    ds.isUnderlineText = false
                }
            }
            val ysSpan = object : ClickableSpan() {
                override fun onClick(widget: View) {
                    ActivityMessenger.startActivityForResult(
                        this@LoginActivity, ProtocolActivity::class,
                        "data" to it.teacher_privacy_agreement,
                        "name" to "隐私政策"
                    ) {
                        if (it != null) {


                        }
                    }

                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = resources?.getColor(R.color._2979ff)!!
                    ds.isUnderlineText = false
                }
            }
            val childSpan = object : ClickableSpan() {
                override fun onClick(widget: View) {
                    ActivityMessenger.startActivityForResult(
                        this@LoginActivity, ProtocolActivity::class,
                        "data" to it.teacher_protect_agreement,
                        "name" to "儿童隐私协议"
                    ) {
                        if (it != null) {


                        }
                    }
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = resources?.getColor(R.color._2979ff)!!
                    ds.isUnderlineText = false
                }
            }

            SpanUtils.with(dataBinding.tvCheck).append("我已阅读并同意").append("《使用协议》、")
                .setClickSpan(useSpan)
                .append("《隐私政策》、")
                .setClickSpan(ysSpan)
                .append("《儿童隐私保护政策》")
                .setClickSpan(childSpan)
                .create()
        }
    }


    override fun isStatusBarDarkFont(): Boolean {
        return true
    }
}